<template>
  <div class="rightBodyContent">
    <div class="rightBodyContent-in">
      <el-tabs v-model="tabType" type="card" @tab-click="handleClick">
        <el-tab-pane label="我的通知" name="0"></el-tab-pane>
        <el-tab-pane label="我的发布" name="1"></el-tab-pane>
        <el-tab-pane label="我的审核" name="1"></el-tab-pane>
      </el-tabs>
<!--      style 1我的发布2我的审核3园长审核 0我的通知-->
<!--      <div class="tab-body mb-10">-->
<!--        <el-radio-group v-model="searchData.status" size="small" @change="changeFn()">-->
<!--          <el-radio-button :label="0">审批中({{ number ? number[0] : 0 }})</el-radio-button>-->
<!--          <el-radio-button :label="1">未下单({{ number ? number[1] : 0 }})</el-radio-button>-->
<!--          <el-radio-button :label="3">未入库({{ number ? number[3] : 0 }})</el-radio-button>-->
<!--          <el-radio-button :label="4">已入库({{ number ? number[4] : 0 }})</el-radio-button>-->
<!--          <el-radio-button :label="2">驳回({{ number ? number[2] : 0 }})</el-radio-button>-->
<!--        </el-radio-group>-->
<!--      </div>-->
      <div class="toplineBox mb-10">
        <div class="toplineBox-left">
          <el-input v-model="searchData.name" placeholder="名称" size="small" class="w-140 mr-10"></el-input>
          <el-button type="primary" size="small" @click="searchFn(1)"><i class="el-icon-search"></i>搜索</el-button>
          <el-button size="small" @click="searchFn(2)"><i class="el-icon-refresh"></i>重置</el-button>
        </div>
        <p class="numberline toplineBox-right">总{{ page.total }}条</p>
      </div>
      <!--本体列表，数据 -->
      <div class="table-body">
        <div class="table-body-box">
          <el-table
              ref="multipleTable"
              :data="arryList"
              :stripe="tableStyle.tableStripe"
              :border="tableStyle.tableBorder"
              tooltip-effect="dark"
              header-cell-class-name="table-text-c"
              style="width: 100%">
            <el-table-column
                prop="name"
                :label="searchData.type==0?'物料名称':'教具名称'"
                align="center"
                :key="1">
            </el-table-column>
            <el-table-column
                prop="need_number"
                label="数量"
                align="center"
                width="60"
                :key="2">
            </el-table-column>
            <el-table-column
                label="单价"
                align="center"
                prop="price"
                width="80"
                :key="3">
            </el-table-column>
            <el-table-column
                prop="finance_source"
                label="资金来源"
                align="center"
                :key="55">
            </el-table-column>
            <el-table-column
                prop="expect_time"
                label="期望交付日期"
                align="center"
                width="110"
                :key="4">
            </el-table-column>
            <el-table-column
                label="申请人"
                align="center"
                :show-overflow-tooltip="true"
                :key="5">
              <template slot-scope="scope">
                {{ scope.row.user ? scope.row.user.realname : '--' }}
              </template>
            </el-table-column>
            <el-table-column
                prop="create_time"
                label="申请时间"
                align="center"
                width="110"
                :key="6">
            </el-table-column>
            <!--            <el-table-column-->
            <!--                label="采购网址"-->
            <!--                align="center"-->
            <!--                width="140"-->
            <!--                :key="7">-->
            <!--              <template slot-scope="scope">-->
            <!--                <div class="url-box-line" v-clipboard:copy="scope.row.url" v-clipboard:success="onCopy"-->
            <!--                     v-clipboard:error="onError" v-if="scope.row.url">-->
            <!--                  <div class="url-box-line-a">{{ scope.row.url }}</div>-->
            <!--                  <i class="el-icon-document-copy"></i>-->
            <!--                </div>-->
            <!--              </template>-->
            <!--            </el-table-column>-->
            <el-table-column
                label="申购照"
                align="center"
                width="70"
                :key="8">
              <template slot-scope="scope">
                <!--                <viewerModle type="1" :imgSrc="scope.row.pic"></viewerModle>-->
                <ImagePreview :src="scope.row.pic" width="40px" height="40px"></ImagePreview>
              </template>
            </el-table-column>
            <el-table-column
                label="入库照"
                align="center"
                :key="9"
                v-if="searchData.status==4">
              <template slot-scope="scope">
                <!--                <viewerModle type="1" :imgSrc="scope.row.put_pic"></viewerModle>-->
                <ImagePreview :src="scope.row.put_pic" width="40px" height="40px"></ImagePreview>
              </template>
            </el-table-column>
            <el-table-column
                prop="status"
                label="状态"
                align="center"
                :key="10">
              <template slot-scope="scope">
                <!--                0待审核1通过2驳回3完成-->
                <!--                此处需要判断显示 -->
                <!--        0待审核1通过2驳回3未入库4已入库-->
                <!--        审批中 未下单 未入库 已入库 驳回-->
                <el-popover
                    placement="left-start"
                    title="审核进程"
                    width="250"
                    trigger="hover"
                    v-if="searchData.status==0 || searchData.status==1|| searchData.status==2">
                  <div class="tableline">
                    <div class="list" v-for="(item,index) in scope.row.examine" :key="'l'+index">
                      <p v-if="!item.status">{{ item.flow_name }}：<i>{{ item.status_text }}</i></p>
                      <p v-else>
                        {{ item.flow_name }}：
                        <i :style="{color: item.status==1?'green':'red'}">{{
                            item.status == 1 ? (item.examineuser ? item.examineuser.realname + ',通过(' + item.update_time + ')' : '') : (item.examineuser ? item.examineuser.realname + '，驳回(' + item.content + ')' : '')
                          }}</i></p>
                    </div>
                  </div>
                  <!--                  <span slot="reference" class="tablelineBtn">{{-->
                  <!--                      scope.row.status == 0 ? '待审批' : (scope.row.status == 1 ? '已审批' : '已驳回')-->
                  <!--                    }}</span>-->
                  <el-link :underline="false" slot="reference" v-if="!scope.row.status" type="primary">
                    待审批
                  </el-link>
                  <el-link :underline="false" v-else slot="reference"
                           :type="scope.row.status==2?'danger':'success'">
                    {{ scope.row.status == 2 ? '已驳回' : '待下单' }}
                  </el-link>
                </el-popover>
                <el-popover
                    placement="left-start"
                    title="申购进程"
                    width="250"
                    trigger="hover"
                    v-if="searchData.status==3 || searchData.status==4">
                  <div class="tableline">
                    <div class="list" v-for="(item, index) in scope.row.examine" :key="'ll'+index">
                      <p v-if="!item.status">{{ item.flow_name }}：<i>{{ item.status_text }}</i></p>
                      <p v-else>{{ item.flow_name }}：<i
                          :style="{color: item.status==1?'green':'red'}">{{
                          item.status == 1 ? (item.examineuser ? item.examineuser.realname + '，通过 （' + getTimePut(item.update_time) + '）' : '') : (item.examineuser ? item.examineuser.realname + '，驳回(' + item.content + ')' : '')
                        }}</i></p>
                    </div>
                    <p class="list">
                      <span>下单时间：<i>{{ scope.row.pay_time }}</i></span>
                    </p>
                    <p class="list" v-if="searchData.status==4">
                      <span>入库时间：<i>{{ scope.row.actual_time }}</i></span>
                    </p>
                  </div>
                  <!--                  <span slot="reference" class="tablelineBtn">{{ searchData.status == 3 ? '未入库' : '已入库' }}</span>-->
                  <el-link :underline="false" slot="reference" type="primary">
                    {{ searchData.status == 3 ? '待入库' : '已入库' }}
                  </el-link>
                  <!--                  <el-link :underline="false" v-else slot="reference"-->
                  <!--                           :type="scope.row.status==2?'danger':'success'">-->
                  <!--                    {{ scope.row.status == 2 ? '已驳回' : '已审批' }}-->
                  <!--                  </el-link>-->
                </el-popover>
              </template>
            </el-table-column>
            <el-table-column
                prop="status"
                label="审核流程"
                align="center"
                width="120"
                :key="11">
              <template slot-scope="scope">
                <el-popover
                    placement="left-start"
                    :title="flowName"
                    width="300"
                    @show="showPopover(scope.row.flow_id)"
                    trigger="click">
                  <div class="tableline">
                    <div class="examine-list" v-for="(item,index) in flowList" :key="'s'+index">
                      <div class="examine-list-title">{{ item.name }}：</div>
                      <div class="examine-list-user">
                        <el-tag
                            :disable-transitions="false"
                            class="mr-5"
                            size="small"
                            v-for="(items,index) in item.users"
                            :key="'user'+index"
                        >{{ items.realname }}
                        </el-tag>
                      </div>
                    </div>
                  </div>
                  <span slot="reference" class="tablelineBtn">查看审核流程</span>
                </el-popover>
              </template>
            </el-table-column>
            <el-table-column
                prop="status"
                label="操作"
                align="center"
                width="90"
                fixed="right"
                :key="12">
              <template slot-scope="scope">
                <el-button size="mini" @click="godeDetail(scope.row)" class="ml-10">
                  详情
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <!--工具条 分页-->
      <el-col :span="24" class="toolbar">
        <el-pagination
            background
            layout="total, prev, pager, next"
            :total="page.total"
            :page-size="page.pagesize"
            @current-change="handleSizeChange">
        </el-pagination>
      </el-col>
    </div>
<!--    <detail :detailId="detailId" :visible.sync="detailVisible" pageType="search"></detail>-->
  </div>
</template>

<script>
import {pagination, tableoption} from '@/utils/util.js'
import {purchaseDetail} from "@/api/admin/purchase"
// import detail from "./detail";

export default {
  components: {
    // detail
  },
  data() {
    return {
      applyDetail: false,
      detailId: 0,
      handleSelection: [],
      arryList: [], // 页面数据列表
      // 设置table样式
      tableStyle: tableoption,
      // 设置table页面分页所需
      page: {
        pageindex: pagination.pageIndex,
        pagesize: pagination.pageSize,
        total: pagination.total
      },
      iframeSrc: '',
      previewVisible: '',
      number: null,
      searchData: {
        name: '',
        user_name: '',
        start_time: '',
        end_time: '',
        status: 0, // 0待审核 1通过 2驳回 3未入库 4已入库
        type: '0', // 0 物料 1 教具
        p: 1
      },
      // <!--        0待审核1通过2驳回3未入库4已入库-->
      // <!--        审批中 未下单 未入库 已入库 驳回-->
      statusList: {
        0: '待审批',
        1: '未下单',
        2: '驳回申请',
        3: '未入库',
        4: '已入库'
      },
      value: '1',
      addtime: '',
      tabType: '0',
      detailVisible: null,
      flowName: '',
      flowList: []
    }
  },
  created() {
    // this.InitFn()
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleClick(option) {
      if (Number(option.name) !== Number(this.searchData.type)) {
        this.searchData.type = option.name
        this.InitFn()
      }
    },
    searchFn(option) {
      this.page.pageindex = 1
      if (option === 1) {
        if (this.addtime) {
          this.searchData.start_time = this.addtime[0];
          this.searchData.end_time = this.addtime[1];
        } else {
          this.searchData.start_time = '';
          this.searchData.end_time = '';
        }
      } else {
        this.searchData.name = '';
        this.searchData.user_name = '';
        this.searchData.start_time = '';
        this.searchData.end_time = '';
      }
      console.log('收到搜索数据：', option);
      this.InitFn()
    },
    // 初始化
    InitFn() {
      // type 2 审核人员
      // status 	整型 	必填 	1 	- 	状态0待审核1待入库2驳回3已入库
      // type 	整型 	必填 	1 	- 	类型0物料1教具
      // <!--        0待审核1通过2驳回3未入库4已入库-->
      // <!--        审批中 未下单 未入库 已入库 驳回-->
      // let option_ = {}
      this.searchData.p = this.page.pageindex
      this.$fetch(this.api.purchase.list, this.searchData).then((response) => {
        if (response.code === 0) {
          for (let item of response.list.data) {
            item.create_time = item.create_time.split(' ')[0]
            let status_apply = 0
            // 需要查询个人的审核状态
            // 查找属于本账户的状态 this.userInfo
            for (let items of item.examine) {
              if(!status_apply){
                if(items.status===0){
                  items.status_text = '待审批'
                }
                if(items.status===1){
                  items.status_text = '已审批'
                }
                if(items.status===2){
                  items.status_text = '已驳回'
                  status_apply = 1
                }
              }else{
                items.status_text = '终止'
              }
              if (items.status == 2) {
                status_apply = 1
              }
            }
          }
          this.arryList = JSON.parse(JSON.stringify(response.list.data))
          this.page.total = response.list.total
          this.number = response.number
        } else {
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    examinBtn(type, id) {
      let that = this;
      if (type === 1) {
        this.$confirm('确认公章的审核, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          that.subimitExamin(type, id)
        }).catch(() => {

        });
      } else {
        this.$prompt('输入驳回说明', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
        }).then(({value}) => {
          // value
          that.subimitExamin(type, id, value)
        }).catch(() => {

        });
      }
    },
    changeFn() {
      this.InitFn()
    },
    // 分页
    handleSizeChange(val) {
      this.page.pageindex = val
      this.InitFn()
    },
    // 搜索组件
    subimitExamin(type, id, value) {
      let n = this;
      let option = {id: id, type: type}
      if (type === 2) {
        option.content = value;
      }
      this.$post(this.api.seal.examine, option).then((response) => {
        if (response.code === 0) {
          n.InitFn()
        } else {
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    showFileFn(fileSrc) {
      this.iframeSrc = fileSrc;
      this.previewVisible = true;
    },
    getTimePut(option) {
      return option.split(' ')[0]
    },
    getJson(option) {
      return JSON.parse(JSON.stringify(option))
    },
    godeDetail(option) {
      this.detailId = option.id;
      this.detailVisible = true;
    },
    // 复制成功
    onCopy() {
      this.$message.success('采购网址复制成功')
    },
    // 复制失败
    onError() {
      this.$message.error('采购网址复制失败')
    },
    showPopover(id) {
      this.flowList = []
      purchaseDetail({id: id}).then(res => {
        console.log('res:', res)
        this.flowName = res.vo.title
        if (res.vo.flows) {
          // for (let item of res.vo.flows) {
          //   item.dynamicTagsArray = {};
          //   item.dynamicTagsId = [];
          //   item.dynamicTags = [];
          //   for (let items of item.users) {
          //     item.dynamicTagsArray[items.id] = items
          //     item.dynamicTagsId.push(items.id)
          //     item.dynamicTags.push(items.realname)
          //   }
          // }
          // this.title = res.vo.title
          // this.examineList = this.getJson(res.vo.flows)

          this.flowList = res.vo.flows
        }
      })
    }
  },
  mounted() {
  },
  // 缓存机制 end
}
</script>


<style scoped>

.wx-user-box-line img {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  margin-right: 5px;
}

.wx-user-box-line {
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn-wx {
  cursor: pointer;
  padding: 5px;
}

.btn-wx:hover {
  color: #1b8de5;
  text-decoration: underline;
}

.delet-wx {
  color: red;
  cursor: pointer;
}

.delet-wx:hover {
  text-decoration: underline;
}

.tab-body {
  display: flex;
  align-items: center;
}

.tab-box {
  flex: 1;
}

.tab-box-list {
  display: flex;
  align-items: center;
}

.examinBtn {
  display: flex;
  flex-direction: column;
  align-items: center;
  color: red;
}

.examinBtn i {
  font-style: normal;
  color: #1b8de5;
  cursor: pointer;
  font-size: 12px;
}

.tableline .list {
  font-size: 12px;
}

.tableline .list i {
  font-style: normal;
}

.url-box-line {
  display: flex;
  align-items: center;
  color: #3e89ff;
}

.url-box-line:hover {
  text-decoration: underline;
}

.url-box-line .url-box-line-a {
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}
.examine-list{
  display: flex;
  padding: 10px 0;
}
.examine-list-title{
  width: 85px;
}
.examine-list-user{
  flex: 1;
}
</style>
